package com.demo.webflux.domain;

import java.io.Serializable;

import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "wallet")
public class Wallet implements Serializable {

	private static final long serialVersionUID = -766601834906091680L;
	@Id
	private ObjectId id;
	private String owner; // 钱包拥有者
	private int balance; // 账户余额

	/**检查钱包中是否有足够的资金进行操作
	 * @param amount
	 * @return
	 */
	public boolean hasEnoughFunds(int amount) {
		return balance >= amount;
	}

	/**按要求的金额减少钱包的余额
	 * @param amount
	 */
	public void withdraw(int amount) {
		if(!hasEnoughFunds(amount)) {
			throw new IllegalStateException("not nough funds!");
		}
		this.balance = this.balance - amount;
	}

	/**按要求的金额增加钱包的余额
	 * @param amount
	 */
	public void deposit(int amount) {
		this.balance = this.balance + amount;
	}

	public ObjectId getId() {
		return id;
	}
	public void setId(ObjectId id) {
		this.id = id;
	}
	public String getOwner() {
		return owner;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	public int getBalance() {
		return balance;
	}
	public void setBalance(int balance) {
		this.balance = balance;
	}

}
